package com.wm.wmim.server.service.impl;
import java.time.LocalDateTime;

import com.wm.wmim.common.enums.CommonBizCodeEnum;
import com.wm.wmim.common.exception.BusinessException;
import com.wm.wmim.server.dao.FriendListDao;
import com.wm.wmim.server.dbservice.ChatListDbService;
import com.wm.wmim.server.dbservice.UserInfoDbService;
import com.wm.wmim.server.dto.bo.req.AddFriendReqBO;
import com.wm.wmim.server.entity.ChatListDO;
import com.wm.wmim.server.entity.FriendListDO;
import com.wm.wmim.server.entity.UserInfoDO;
import com.wm.wmim.server.service.ChatService;
import com.wm.wmim.server.service.FriendListService;
import com.wm.wmim.server.dbservice.FriendListDbService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 好友列表表(FriendList)表服务实现类
 *
 * @author weiming
 * @since 2025-02-20 23:24:16
 */
@Service
public class FriendListServiceImpl implements FriendListService {

    @Resource
    private FriendListDao friendListDao;
    @Resource
    private FriendListDbService friendListDbService;
    @Resource
    private UserInfoDbService userInfoDbService;
    @Resource
    private ChatListDbService chatListDbService;
    @Resource
    private ChatService chatService;


    @Override
    public Boolean addFriend(AddFriendReqBO addFriendReqBO) {
        UserInfoDO friendUserInfoDO = userInfoDbService.getByUserCode(addFriendReqBO.getFriendCode());
        if (friendUserInfoDO == null) {
            throw new BusinessException(CommonBizCodeEnum.USER_NOT_EXIST);
        }
        // TODO 加好友
        FriendListDO friendListDO = new FriendListDO();
        friendListDO.setUserId(addFriendReqBO.getCurrentUserId());
        friendListDO.setFriendId(friendUserInfoDO.getId());

        friendListDbService.save(friendListDO);
        // 先在聊天列表加一个得了
        ChatListDO chatListDO = new ChatListDO();
        chatListDO.setUserId(addFriendReqBO.getCurrentUserId());
        chatListDO.setFriendId(friendUserInfoDO.getId());
        chatListDO.setLastMessageContent(addFriendReqBO.getMessageContent());
        chatListDO.setLastMessageTime(LocalDateTime.now());
        chatListDbService.save(chatListDO);
        // TODO 发送消息
//        chatService.sendPrivateMessage()
        return true;
    }
}

